package com.rnd.kx.mvp.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.adapter.IndexAdapter;
import com.rnd.kx.mvp.base.BaseMvpActivity;
import com.rnd.kx.mvp.bean.IndexBean;
import com.rnd.kx.mvp.bean.LoginBean;
import com.rnd.kx.mvp.bean.SearchBean;
import com.rnd.kx.mvp.contract.MainContract;
import com.rnd.kx.mvp.dialog.ProgressDialog;
import com.rnd.kx.mvp.http.bean.BaseObjectBean;
import com.rnd.kx.mvp.presenter.MainPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.View {

    @BindView(R.id.et_username_login)
    EditText etUsernameLogin;
    @BindView(R.id.et_password_login)
    EditText etPasswordLogin;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    private IndexAdapter adapter;
    private ImageLoader imageLoader;
    DisplayImageOptions options;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mPresenter = new MainPresenter();
        mPresenter.attachView(this);

        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(this));
        options = new DisplayImageOptions.Builder()
                //.showStubImage(R.drawable.img_default) // 在ImageView加载过程中显示图片
                //.showImageForEmptyUri(R.drawable.img_default) // image连接地址为空时
                //.showImageOnFail(R.drawable.img_default) // image加载失败
                .cacheInMemory(true) // 加载图片时会在内存中加载缓存
                .cacheOnDisc(true) // 加载图片时会在磁盘中加载
                .build();

    }


    @OnClick(R.id.btn_signin_login)
    public void onViewClicked() {
        /*if (getUsername().isEmpty() || getPassword().isEmpty()) {
            Toast.makeText(this, "帐号密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }*/
        //mPresenter.login(getUsername(), getPassword());
        //mPresenter.getSearch(getUsername());
        mPresenter.getIndex();
    }

    private String getPassword() {
        return etPasswordLogin.getText().toString().trim();
    }

    private String getUsername() {
        return etUsernameLogin.getText().toString().trim();
    }



    @Override
    public void showLoading() {
        //ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        //ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void loginResult(BaseObjectBean<LoginBean> bean) {

    }

    @Override
    public void searchResult(BaseObjectBean<SearchBean> bean) {
        Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void indexResult(BaseObjectBean<IndexBean> bean) {
        adapter = new IndexAdapter(bean.getData(), this,imageLoader);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
    }
}
